import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/widgets/screen_state_widget.dart';

import '../../../data/remote/launcher_url_in_platform.dart';
import '../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../widgets/app_bar/custom_app_bar.dart';
import '../../../widgets/app_bar/outline_icon_button.dart';
import '../models/link_model.dart';
import 'links_cubit.dart';
import 'package:flutter/material.dart';


class LinksScreen extends StatelessWidget {
  final bool? isPublic;
  const LinksScreen({Key? key, this.isPublic = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: _buildAppBar(context:context),
      body: BlocProvider<LinksCubit>(
        create: (context) => LinksCubit()..init(isPublic: isPublic),
        child: BlocConsumer<LinksCubit, LinksState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            var cubit = LinksCubit.get(context);

            return SizedBox(
              height: height,
              width: width,
              child: ScreenStateWidget(
                isLoading: state is LinksLoading,
                isError: state is LinksError,
                isDone: state is LinksDone,
                onRefresh: cubit.getAllNotes,
                onDone: Column(

                  children: [
                    Expanded(
                      child: ListView.builder(
                        itemCount: cubit.links.length,
                        itemBuilder: (context, index) {
                          final note = cubit.links[index];
                          return _buildNoteItem(note);
                        },
                      ),
                    ),
                    const SizedBox(height: 32),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget _buildNoteItem(LinkModel myLink) {
    const String dateTimeFormatPattern = 'yyyy-MM-dd hh:mm';

    return Slidable(
      enabled:PrefUtils.isAdmin(),
      startActionPane: ActionPane(
        motion: const ScrollMotion(),
        children: [
          SlidableAction(
            onPressed: (context){
              //showDeleteConfirmation(context, gym);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.red,
            icon: Icons.delete,
            label: 'حذف',
          ),

        ],
      ),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: ListTile(
          leading: Image.network(myLink.icon),
          title: Text(myLink.title??""),
          onTap: (){
            launchInBrowser(Uri.parse(myLink.link));
          },

        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context}) {
    return CustomAppBar(
      leading: OutlineIconButton(
        imagePath: ImageConstant.imgRightIcon,
        onTapped: (){
          Navigator.pop(context);
        },
      ),
      title: AppbarSubtitle(
        text: "تابعنا", margin: EdgeInsets.only(left: 12.h),
      ),
    );
  }

}
